import socket
from concurrent.futures import ThreadPoolExecutor


class TCPServer:
    def __init__(self):
        self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.sock.bind(('127.0.0.1',7799))
        print("服务器建立了链接：127.0.0.1:7799")
        self.sock.listen(100)

    def handle_request(self,cil_sock,addr):
        print(f"客户端{addr}建立了链接")
        content=cil_sock.recv(1024).decode()
        print("客户端发来的请求数据为：",content)
        header = 'HTTP/1.1 200 OK\r\n'
        header += 'Content-Type: text/html;charset=utf-8\r\n'
        header += '\r\n\r\n'
        body = '恭喜你请求成功'
        data=header+body
        cil_sock.send(data.encode())

    def run(self):
        with ThreadPoolExecutor(max_workers=20) as pool:
            while True:
                cil_sock,addr=self.sock.accept()
                pool.submit(self.handle_request,cil_sock,addr)



if __name__ == '__main__':
    TCPServer().run()